// productDetail.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {

        indicatorDots: true,
        autoplay: false,
        interval: 5000,
        duration: 1000,
        iconCaret: "True",
        iconShangla: "False",
        acount: 1,
        ids: [],
        points: 0
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this

        wx.request({
            url: app.data.baseUrl + '/AibraUsb/goods/detail.do',
            data: {
                goodsId: options.goodsId,
                openId: app.globalData.openid
            },
            success: function(res) {
                console.log('resssssLLL', res)
                var ids = [];

                for (var i = 0; i < res.data.priceList.length; i++) {
                    ids[i] = '';
                }
                ids[0] = 'checked'
                console.log(res.data.priceList[0][2] / 10)
                var points = Math.ceil(res.data.priceList[0][2] / 10)
                that.setData({
                    goods: res.data.goods,
                    lbList: res.data.lbList,
                    priceList: res.data.priceList[0],
                    priceLists: res.data.priceList,
                    xqList: res.data.xqList,
                    goodsId: options.goodsId,
                    ids: ids,
                    points: points
                })
            }
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    bindiconCaret: function() {
        this.setData({
            iconCaret: "False",
            iconShangla: "True"
        })
    },
    bindiconShangla: function() {
        this.setData({
            iconCaret: "True",
            iconShangla: "False"
        })
    },
    fastBuy: function() {

        var that = this
        var openId = app.globalData.openid
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/orders/addUI.do',
            data: {
                openId: app.globalData.openid,
                priceId: that.data.priceList[0],
                goodsId: that.data.goodsId,
                count: that.data.acount
            },
            success: function(res) {
                var points = that.data.priceList[3] || 0


                if (that.data.acount * points <= res.data.totalPoints) {
                    wx.navigateTo({
                        url: '../personal/firmOrder/firmOrder?priceId=' + that.data.priceList[0] + '&goodsId=' + that.data.goodsId + '&count=' + that.data.acount + '&content=' + that.data.goods.content
                    })

                } else {
                    wx.showToast({
                        title: '积分不够'
                    })
                }

            }
        })
    },
    checkedstyle: function(e) {
        var that = this;
        var ids = that.data.ids;

        var currentIndex = e.currentTarget.dataset.index;
        console.log(currentIndex)
        for (var i = 0; i < ids.length; i++) {
            ids[i] = ''
        }
        ids[currentIndex] = 'checked';
        var points = Math.ceil(that.data.priceLists[currentIndex][2] / 10)
        that.setData({
            ids: ids,
            priceList: that.data.priceLists[currentIndex],
            points: points
        })
    },
    minus: function() {
        var that = this;
        let acount = that.data.acount;
        if (acount > 1) {
            --acount
        } else {
            acount = acount
        }
        that.setData({
            acount: acount
        })
    },
    plus: function() {
        var that = this;
        let max = that.data.priceList[4]
        let acount = that.data.acount;
        if (acount < max) {
            ++acount
        } else {
            acount = acount
        }
        that.setData({
            acount: acount
        })
    },
})